package DataStorage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;

import HibernateSQLite.java.model.CodageChemin;

public class CodageCheminDAOHibernate extends
		GenericDAOHibernate<CodageChemin, Long, ICodageCheminDAO> implements
		ICodageCheminDAO {

	@SuppressWarnings("unchecked")
	public boolean isExist(String OP, String chemin, long InformationLigne_id) {
		try {
			List<CodageChemin> list = getSession()
					.createCriteria(CodageChemin.class)
					.add(Restrictions.eq("OP", OP))
					.add(Restrictions.eq("chemin", chemin))
					.add(Restrictions.eq("InformationLigne_id",
							InformationLigne_id)).list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isExistInformationLigne_id(long InformationLigne_id) {
		try {
			List<CodageChemin> list = getSession()
					.createCriteria(CodageChemin.class)
					.add(Restrictions.eq("InformationLigne_id",
							InformationLigne_id)).list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	public CodageChemin getCodageChemin(String OP, long InformationLigne_id) {
		try {
			List<CodageChemin> list = getSession()
					.createCriteria(CodageChemin.class)
					.add(Restrictions.eq("OP", OP))
					.add(Restrictions.eq("InformationLigne_id",
							InformationLigne_id)).list();

			for (CodageChemin c : list) {
				return c;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<CodageChemin> getListCodageChemin(long InformationLigne_id) {
		try {
			List<CodageChemin> list = getSession()
					.createCriteria(CodageChemin.class)
					.add(Restrictions.eq("InformationLigne_id",
							InformationLigne_id)).list();

			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	public List<String> getListOP_2(long InformationLigne_id) {
		try{
			List<CodageChemin> list = this.getListCodageChemin(InformationLigne_id);
			List<String> listOP_2 = new ArrayList<String>();
			for (CodageChemin c : list) {
				listOP_2.add(c.getOP_2());
			}
			return listOP_2;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;		
	}

	public List<String> getListOP(long InformationLigne_id) {
		try{
			List<CodageChemin> list = this.getListCodageChemin(InformationLigne_id);
			List<String> listOP = new ArrayList<String>();
			for (CodageChemin c : list) {
				listOP.add(c.getOP());
			}
			return listOP;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;			
	}

	public List<String> getListChemin(long InformationLigne_id) {
		try{
			List<CodageChemin> list = this.getListCodageChemin(InformationLigne_id);
			List<String> listChemin = new ArrayList<String>();
			for (CodageChemin c : list) {
				listChemin.add(c.getChemin());
			}
			return listChemin;
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;	
		
	}

	public String getOPByOP_2(String OP_2, long InformationLigne_id) {
		try{
			List<CodageChemin> list = this.getListCodageChemin(InformationLigne_id);
			String OP = "";
			for (CodageChemin c : list) {
				if (c.getOP_2().equals(OP_2)) {
					OP = c.getOP();
				}
			}
			return OP;
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
		
	}

	@Override
	public void saveCodageChemin(List<String> OP, List<String> OP_2,
			List<String> chemin, long InformationLigne_id) {
		try{
			CodageChemin codageChemin = null;
			int sizeOP = OP.size();
			int sizeOP_2 = OP_2.size();
			int sizeChemin = chemin.size();
			if (sizeOP == sizeChemin) {
				String[] arr_OP = (String[]) OP.toArray(new String[sizeOP]);
				String[] arr_OP_2 = (String[]) OP_2.toArray(new String[sizeOP_2]);
				String[] arr_chemin = (String[]) chemin
						.toArray(new String[sizeChemin]);

				// String[][] arr_codageChemin = new String[2][sizeChemin];
				for (int i = 0; i < sizeOP; i++) {
					codageChemin = new CodageChemin();
					codageChemin.setOP(arr_OP[i]);
					codageChemin.setOP_2(arr_OP_2[i]);
					codageChemin.setChemin(arr_chemin[i]);
					codageChemin.setInformationLigne_id(InformationLigne_id);
					// DB operation
					getSession().save(codageChemin);
				}

			} else {
				System.out
						.println("Erreur d'op�ration,OP SIZE != Chemin SIZE ,r�-entrer les donn�es s'il vous pla�t !");
			}

			this.commit();
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		

	}

	public void deleteCodageChemin(long InformationLigne_id) {
		try{
			if (this.isExistInformationLigne_id(InformationLigne_id)) {
				List<CodageChemin> listCodageCheminDAO = getListCodageChemin(InformationLigne_id);
				this.deleAll(listCodageCheminDAO);
			}
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		
	}

	@SuppressWarnings({ "unused" })
	public static void main(String[] args) {

		List<String> OP = Arrays.asList("100", "110", "120", "130", "140");
		List<String> OP_2 = Arrays.asList("100", "120", "", "", "");
		List<String> chemin = Arrays.asList("1", "1,2", "3", "4", "5");
		long infoLinge = 10;
		long InformationLigne_id = 122222;

		CodageCheminDAOHibernate c = new CodageCheminDAOHibernate();
		c.saveCodageChemin(OP, OP_2, chemin, InformationLigne_id);
		c.deleteCodageChemin(InformationLigne_id);

	}

}
